/*
 * Copyright (c) ILOG 1995-2008. All rights reserved.
 * 
 * This source code is intended only as a supplement to ILOG's product
 * documentation.
 * 
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 */

package loan;

import java.util.Date;

public class Loan extends LoanUtil implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7356529391378635986L;

	@SuppressWarnings("unused")
	private Loan() {
	}

	public Loan(Date startDate, int numberOfMonthlyPayments, int amount,
			double loanToValue) {
		this.startDate = startDate;
		this.numberOfMonthlyPayments = numberOfMonthlyPayments;
		this.amount = amount;
		this.loanToValue = loanToValue;
	}

	//Fields
	private int numberOfMonthlyPayments;

	private Date startDate;

	private int amount;

	private double loanToValue;

	private double yearlyInterestRate;

	private double monthlyRepayment;

	/**
	 * @return Returns the loanToValue.
	 */
	public double getLoanToValue() {
		return loanToValue;
	}

	/**
	 * @param loanToValue
	 *            The loanToValue to set.
	 */
	public void setLoanToValue(double loanToValue) {
		this.loanToValue = loanToValue;
	}

	/**
	 * @return Returns the yearlyRepayment.
	 */
	public double getYearlyRepayment() {
		return 12*monthlyRepayment;
	}

	/**
	 * @return Returns the monthlyRepayment.
	 */
	public double getMonthlyRepayment() {
		return monthlyRepayment;
	}
	/**
	 * @param monthlyRepayment
	 *            The monthlyRepayment to set.
	 */
	public void setMonthlyRepayment(double monthlyRepayment) {
		this.monthlyRepayment = monthlyRepayment;
	}

	/**
	 * @return Returns the rate.
	 */
	public double getYearlyInterestRate() {
		return yearlyInterestRate;
	}

	/**
	 * @param rate
	 *            The rate to set.
	 */
	public void setYearlyInterestRate(double rate) {
		this.yearlyInterestRate = rate;
	}

	/**
	 * @return Returns the amount.
	 */
	public int getAmount() {
		return amount;
	}

	/**
	 * @return Returns the numberOfMonthlyPayments.
	 */
	public int getNumberOfMonthlyPayments() {
		return numberOfMonthlyPayments;
	}
	
	/**
	 * Get the loan duration (rounded to the upper int)
	 * @return a number of years 
	 */
	public int getDuration() {
		return (numberOfMonthlyPayments+11)/12;
	}

	/**
	 * @return Returns the startDate.
	 */
	public Date getStartDate() {
		return startDate;
	}
	
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("Loan(");
		buf.append(" amount: ").append(amount);
		buf.append(" starting: ").append(DateUtil.format(startDate));
		buf.append(" duration: ").append(numberOfMonthlyPayments).append(" month");
		buf.append(" LTV: ").append(formattedPercentage(loanToValue));
		
		if (yearlyInterestRate>0)
		  buf.append(" rate: ").append(formattedPercentage(yearlyInterestRate));
		
		if (monthlyRepayment>0)
		  buf.append(" monthly repayment: ").append(formattedAmount(monthlyRepayment));
		
		buf.append(")");
		return buf.toString();
	}
};
